package com.wl.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wl.mapper.PostMapper;
import com.wl.pojo.PageBean;
import com.wl.pojo.Personal.Collect;
import com.wl.pojo.Post;
import com.wl.pojo.User;
import com.wl.service.PostService;
import com.wl.utils.AliOSSUtils;
import com.wl.utils.Base64ToMultipartFile;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class PostServiceImpl implements PostService {

    @Autowired
    private PostMapper postMapper;

    @Autowired
    private AliOSSUtils aliOSSUtils;

    /**
     * 发布文章接口
     * 将文章数据写入数据库
     */
    @Override
    public void publishPost(Post post) throws IOException {
        // 1、处理文章发布和更新时间
        LocalDateTime nowTime = LocalDateTime.now();
        post.setCreateTime(nowTime);
        post.setUpdateTime(nowTime);
        // 2、处理文章封面图片
        if (post.getCoverImg().length() > 0) {
            String[] parts = post.getCoverImg().split(",");
            MultipartFile multipartFile = new Base64ToMultipartFile(parts[1], parts[0]);
            post.setCoverImg(aliOSSUtils.upload(multipartFile));
        }
        // 3、处理文章其他项并返回文章的ID
        postMapper.publishPost(post);
        // 4、处理文章标签
        for (Integer tagId : post.getTagIds()) {
            postMapper.addPostTag(post.getPostId(), tagId);
        }
    }

    /**
     * 修改文章接口
     * @param post 修改的信息
     */
    @Override
    @Transactional
    public void updatePost(Post post) throws IOException {
        post.setUpdateTime(LocalDateTime.now());
        // 处理文章封面图片
        if(post.getCoverImg().length() > 0 && !post.getCoverImg().split("//")[0].equals("https")) {
            String[] parts = post.getCoverImg().split(",");
            MultipartFile multipartFile = new Base64ToMultipartFile(parts[1], parts[0]);
            post.setCoverImg(aliOSSUtils.upload(multipartFile));
        }
        // 处理文章兴趣标签 先删除旧的标签，再添加新的标签
        postMapper.deletePostTag(post.getPostId());
        for (Integer tagId : post.getTagIds()) {
            postMapper.addPostTag(post.getPostId(), tagId);
        }
        // 执行修改文章操作
        postMapper.updatePost(post);
    }

    /**
     * 获取标签数据
     */
    @Override
    public List<Map<Integer, String>> getTags() {
        return postMapper.getTags();
    }

    /**
     * 获取首页展示的文章数据
     * @return
     */
    @Override
    public PageBean getPosts(Integer userId, Integer categoryId, Integer isDescending, Integer PostCurrentPage, Integer PostPageSize) {
        // 设置分页参数
        PageHelper.startPage(PostCurrentPage, PostPageSize);
        // 执行查询
        List<Post> posts = postMapper.getPosts(userId, categoryId,isDescending);
        for (Post post : posts) {
            post.setTagNames(postMapper.getPostTagNames(post.getPostId()));
            log.info("PostTagNames: {}", post.getTagNames());
        }
        Page<Post> p = (Page<Post>) posts;
        // 封装 PageBean对象
        return new PageBean(p.getTotal(), p.getResult());
    }

    /**
     * 根据浏览量和点赞量进行降序排序查询热点数据
     */
    @Override
    public List<Post> getHotPosts() {
        return postMapper.getHotPosts();
    }

    /**
     * 根据ID查询文章的详细信息
     */
    @Override
    public Post getPostById(Integer postId) {
        Post post = postMapper.getPostById(postId);
        post.setTagNames(postMapper.getPostTagNames(postId));
        return post;
    }

    /**
     * 对文章的访问量加一
     * @param postId 文章id
     */
    @Override
    public void addPostPageview(Integer postId) {
        postMapper.addPostViews(postId);
    }

    /**
     * 根据文章id查询文章的作者信息
     */
    @Override
    public User getUserByPostId(Integer postId) {

        // 查询基本信息，包含粉丝数量
        User userinfo = postMapper.getUserByPostId(postId);

        // 查询文章数量
        Integer postCount = postMapper.getPostCount(userinfo.getUserId());

        // 查询用户标签
        List<String> userTags = postMapper.getUserTags(userinfo.getUserId());

        // 组合数据
        userinfo.setPostCount(postCount);
        userinfo.setUserTags(userTags);

        return userinfo;
    }

    /**
     * 查看某个用户是否关注了文章作责
     */
    @Override
    public Map<String, Boolean> getIsFollow(Integer userId, Integer followId) {
        Integer isFollow = postMapper.getIsFollow(userId, followId);

        Map<String, Boolean> map = new HashMap<>();

        if(isFollow != 0) {
            map.put("isFollow", true);
        } else {
            map.put("isFollow", false);
        }

        return map;
    }

    /**
     * 关注某个用户
     */
    @Override
    public void activeFollow(Integer userId, Integer followId) {
        postMapper.activeFollow(userId, followId);
    }

    /**
     * 取消关注某个用户
     */
    @Override
    public void cancelFollow(Integer userId, Integer followId) {
        postMapper.cancelFollow(userId, followId);
    }

    /**
     * 判断某个用户是否对某篇文章点赞
     */
    @Override
    public Map<String, Boolean> getIsLike(Integer userId, Integer postId) {
        Integer isLike = postMapper.getIsLike(userId, postId);

        Map<String, Boolean> map = new HashMap<>();

        if(isLike != 0) {
            map.put("isLike", true);
        } else {
            map.put("isLike", false);
        }

        return map;
    }

    /**
     * 点赞请求
     */
    @Override
    public void activeLike(Integer userId, Integer postId) {
        postMapper.activeLike(userId, postId);
    }

    /**
     * 取消点赞请求
     */
    @Override
    public void cancelLike(Integer userId, Integer postId) {
        postMapper.cancelLike(userId, postId);
    }

    /**
     * 获取文章有多少点赞数量
     */
    @Override
    public Integer getLikeCount(Integer postId) {
        return postMapper.getLikeCount(postId);
    }

    /**
     * 收藏请求
     */
    @Override
    public void activeCollect(Integer userId, Integer postId, Integer categoryId) {
        postMapper.activeCollect(userId, postId, categoryId);
    }

    /**
     * 取消收藏接口
     */
    @Override
    public void cancelCollect(Integer userId, Integer postId, Integer categoryId) {
        postMapper.cancelCollect(userId, postId, categoryId);
    }

    /**
     * 查看收藏数量
     */
    @Override
    public Integer getCollectCount(Integer postId) {
        return postMapper.getCollectCount(postId);
    }

    /**
     * 判断某个用户是否对某篇文章收藏
     */
    @Override
    public Map<String, Boolean> getIsCollect(Integer userId, Integer postId) {
        Integer isCollect = postMapper.getIsCollect(userId, postId);

        Map<String, Boolean> map = new HashMap<>();

        if(isCollect != 0) {
            map.put("isCollect", true);
        } else {
            map.put("isCollect", false);
        }

        return map;
    }

    /**
     * 创建收藏夹
     * @param collect 创建需要的数据
     */
    @Override
    public void createCategory(Collect collect) {
        collect.setUpdateTime(LocalDateTime.now());
        postMapper.createCategory(collect);
    }
}
